<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="${comment}" prop="contractId">-->
<!--        <el-input-->
<!--          v-model="queryParams.contractId"-->
<!--          placeholder="请输入${comment}"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="合同名称" prop="contractName">
        <el-input
          v-model="queryParams.contractName"
          placeholder="请输入合同名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="客户ID" prop="customerId">-->
<!--        <el-input-->
<!--          v-model="queryParams.customerId"-->
<!--          placeholder="请输入客户ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="商机ID" prop="businessId">-->
<!--        <el-input-->
<!--          v-model="queryParams.businessId"-->
<!--          placeholder="请输入商机ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="审核记录ID" prop="examineRecordId">-->
<!--        <el-input-->
<!--          v-model="queryParams.examineRecordId"-->
<!--          placeholder="请输入审核记录ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="下单日期" prop="orderDate">
        <el-date-picker clearable
          v-model="queryParams.orderDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择下单日期">
        </el-date-picker>
      </el-form-item>
<!--      <el-form-item label="创建人ID" prop="createUserId">-->
<!--        <el-input-->
<!--          v-model="queryParams.createUserId"-->
<!--          placeholder="请输入创建人ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="负责人ID" prop="ownerUserId">-->
<!--        <el-input-->
<!--          v-model="queryParams.ownerUserId"-->
<!--          placeholder="请输入负责人ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="合同编号" prop="num">
        <el-input
          v-model="queryParams.num"
          placeholder="请输入合同编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="合同金额" prop="money">
        <el-input
          v-model="queryParams.money"
          placeholder="请输入合同金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="整单折扣" prop="discountRate">
        <el-input
          v-model="queryParams.discountRate"
          placeholder="请输入整单折扣"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品总金额" prop="totalPrice">
        <el-input
          v-model="queryParams.totalPrice"
          placeholder="请输入产品总金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同类型" prop="yesapi72crmCrmContractTypes">
        <el-input
          v-model="queryParams.yesapi72crmCrmContractTypes"
          placeholder="请输入合同类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="批次 比如附件批次" prop="batchId">-->
<!--        <el-input-->
<!--          v-model="queryParams.batchId"-->
<!--          placeholder="请输入批次 比如附件批次"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="客户签约人" prop="contactsId">-->
<!--        <el-input-->
<!--          v-model="queryParams.contactsId"-->
<!--          placeholder="请输入客户签约人"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="公司签约人" prop="companyUserId">-->
<!--        <el-input-->
<!--          v-model="queryParams.companyUserId"-->
<!--          placeholder="请输入公司签约人"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:contract:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:contract:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:contract:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:contract:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contractList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="${comment}" align="center" prop="id" />-->
<!--      <el-table-column label="${comment}" align="center" prop="contractId" />-->
      <el-table-column label="合同名称" align="center" prop="contractName" />
      <el-table-column label="客户ID" align="center" prop="customerId" />
      <el-table-column label="商机ID" align="center" prop="businessId" />
<!--      <el-table-column label="0 未审核 1 审核通过 2 审核拒绝 3 审核中 4 已撤回 5草稿 6 作废" align="center" prop="checkStatus" />-->
      <el-table-column label="审核记录ID" align="center" prop="examineRecordId" />
      <el-table-column label="下单日期" align="center" prop="orderDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人ID" align="center" prop="createUserId" />
      <el-table-column label="负责人ID" align="center" prop="ownerUserId" />
      <el-table-column label="合同编号" align="center" prop="num" />
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同金额" align="center" prop="money" />
      <el-table-column label="整单折扣" align="center" prop="discountRate" />
      <el-table-column label="产品总金额" align="center" prop="totalPrice" />
      <el-table-column label="合同类型" align="center" prop="yesapi72crmCrmContractTypes" />
      <el-table-column label="付款方式" align="center" prop="paymentType" />
      <el-table-column label="批次 比如附件批次" align="center" prop="batchId" />
      <el-table-column label="只读权限" align="center" prop="roUserId" />
      <el-table-column label="读写权限" align="center" prop="rwUserId" />
      <el-table-column label="客户签约人" align="center" prop="contactsId" />
      <el-table-column label="公司签约人" align="center" prop="companyUserId" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:contract:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:contract:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改ERP-合同对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="${comment}" prop="contractId">-->
<!--          <el-input v-model="form.contractId" placeholder="请输入${comment}" />-->
<!--        </el-form-item>-->
        <el-form-item label="合同名称" prop="contractName">
          <el-input v-model="form.contractName" placeholder="请输入合同名称" />
        </el-form-item>
<!--        <el-form-item label="客户ID" prop="customerId">-->
<!--          <el-input v-model="form.customerId" placeholder="请输入客户ID" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="商机ID" prop="businessId">-->
<!--          <el-input v-model="form.businessId" placeholder="请输入商机ID" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="审核记录ID" prop="examineRecordId">-->
<!--          <el-input v-model="form.examineRecordId" placeholder="请输入审核记录ID" />-->
<!--        </el-form-item>-->
        <el-form-item label="下单日期" prop="orderDate">
          <el-date-picker clearable
            v-model="form.orderDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择下单日期">
          </el-date-picker>
        </el-form-item>
<!--        <el-form-item label="创建人ID" prop="createUserId">-->
<!--          <el-input v-model="form.createUserId" placeholder="请输入创建人ID" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="负责人ID" prop="ownerUserId">-->
<!--          <el-input v-model="form.ownerUserId" placeholder="请输入负责人ID" />-->
<!--        </el-form-item>-->
        <el-form-item label="合同编号" prop="num">
          <el-input v-model="form.num" placeholder="请输入合同编号" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="合同金额" prop="money">
          <el-input v-model="form.money" placeholder="请输入合同金额" />
        </el-form-item>
        <el-form-item label="整单折扣" prop="discountRate">
          <el-input v-model="form.discountRate" placeholder="请输入整单折扣" />
        </el-form-item>
        <el-form-item label="产品总金额" prop="totalPrice">
          <el-input v-model="form.totalPrice" placeholder="请输入产品总金额" />
        </el-form-item>
        <el-form-item label="合同类型" prop="yesapi72crmCrmContractTypes">
          <el-input v-model="form.yesapi72crmCrmContractTypes" placeholder="请输入合同类型" />
        </el-form-item>
        <el-form-item label="批次 比如附件批次" prop="batchId">
          <el-input v-model="form.batchId" placeholder="请输入批次 比如附件批次" />
        </el-form-item>
        <el-form-item label="只读权限" prop="roUserId">
          <el-input v-model="form.roUserId" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="读写权限" prop="rwUserId">
          <el-input v-model="form.rwUserId" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="客户签约人" prop="contactsId">
          <el-input v-model="form.contactsId" placeholder="请输入客户签约人" />
        </el-form-item>
        <el-form-item label="公司签约人" prop="companyUserId">
          <el-input v-model="form.companyUserId" placeholder="请输入公司签约人" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listContract, getContract, delContract, addContract, updateContract } from "@/api/system/contract";

export default {
  name: "Contract",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // ERP-合同表格数据
      contractList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        // contractId: null,
        contractName: null,
        customerId: null,
        businessId: null,
        checkStatus: null,
        examineRecordId: null,
        orderDate: null,
        createUserId: null,
        ownerUserId: null,
        num: null,
        startTime: null,
        endTime: null,
        money: null,
        discountRate: null,
        totalPrice: null,
        yesapi72crmCrmContractTypes: null,
        paymentType: null,
        batchId: null,
        roUserId: null,
        rwUserId: null,
        contactsId: null,
        companyUserId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询ERP-合同列表 */
    getList() {
      this.loading = true;
      listContract(this.queryParams).then(response => {
        this.contractList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        contractId: null,
        contractName: null,
        customerId: null,
        businessId: null,
        checkStatus: null,
        examineRecordId: null,
        orderDate: null,
        createUserId: null,
        ownerUserId: null,
        createTime: null,
        num: null,
        startTime: null,
        endTime: null,
        money: null,
        discountRate: null,
        totalPrice: null,
        yesapi72crmCrmContractTypes: null,
        paymentType: null,
        batchId: null,
        roUserId: null,
        rwUserId: null,
        contactsId: null,
        companyUserId: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加ERP-合同";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getContract(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改ERP-合同";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateContract(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContract(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除ERP-合同编号为"' + ids + '"的数据项？').then(function() {
        return delContract(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/contract/export', {
        ...this.queryParams
      }, `contract_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
